package com.ttl.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ttl.web.entity.Goods;
import com.ttl.web.entity.Suggestion;
import com.ttl.web.entity.TotalVo;
import com.ttl.web.service.GoodsService;
import com.ttl.web.service.SuggestionService;
import com.ttl.web.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import com.ttl.web.utils.ResultVo;

import java.util.List;


/**
 * 后台主页操作接口
 **/
@RestController
@RequestMapping("/api/home")
public class HomeController {

    @Resource
    private GoodsService goodsService;

    @Resource
    private UserService userService;

    @Resource
    private SuggestionService suggestionService;

    /**
     * 统计总数
     */
    @GetMapping("/getTotal")
    public ResultVo getTotal() {
        TotalVo vo = new TotalVo();
        //查询失物总数
        QueryWrapper<Goods> lostQuery = new QueryWrapper<>();
        lostQuery.lambda().eq(Goods::getType, "0");
        int lostCount = (int) goodsService.count(lostQuery);
        vo.setLostCount(lostCount);
        //查询招领总数
        QueryWrapper<Goods> claimQuery = new QueryWrapper<>();
        claimQuery.lambda().eq(Goods::getType, "1");
        int foundCount = (int) goodsService.count(claimQuery);
        vo.setClaimCount(foundCount);
        //查询用户总数
        int count = (int) userService.count();
        vo.setUserCount(count);
        //查询反馈总数
        int count1 = (int) suggestionService.count();
        vo.setSuggestionCount(count1);
        return ResultVo.success("查询成功",vo);
    }

    /**
     * 查询最近反馈
     */
    @GetMapping("/getList")
    public ResultVo getList() {
        QueryWrapper<Suggestion> query = new QueryWrapper<>();
        query.lambda().orderByDesc(Suggestion::getCreateTime)
                .last("limit 11");
        List<Suggestion> list = suggestionService.list(query);
        return ResultVo.success("查询成功",list);
    }

}